// 烟花效果系统
function createFireworks() {
    const container = document.getElementById('fireworks-container');
    if (!container) return;
    
    // 创建多个烟花
    for (let i = 0; i < 5; i++) {
        setTimeout(() => {
            createSingleFirework();
        }, i * 300);
    }
}

function createSingleFirework() {
    const container = document.getElementById('fireworks-container');
    if (!container) return;
    
    // 随机位置
    const x = Math.random() * window.innerWidth;
    const y = Math.random() * (window.innerHeight * 0.6) + window.innerHeight * 0.1;
    
    // 创建烟花爆炸
    createFireworkExplosion(x, y);
}

function createFireworkExplosion(x, y) {
    const colors = [
        '#ff6b9d', '#4ecdc4', '#ffe66d', '#ff8a80', 
        '#82b1ff', '#b388ff', '#ff5722', '#4caf50'
    ];
    
    const particleCount = 30;
    const particles = [];
    
    // 创建粒子
    for (let i = 0; i < particleCount; i++) {
        const particle = document.createElement('div');
        const color = colors[Math.floor(Math.random() * colors.length)];
        
        particle.style.cssText = `
            position: absolute;
            width: 6px;
            height: 6px;
            background: ${color};
            border-radius: 50%;
            pointer-events: none;
            left: ${x}px;
            top: ${y}px;
            box-shadow: 0 0 10px ${color};
        `;
        
        document.getElementById('fireworks-container').appendChild(particle);
        particles.push(particle);
        
        // 计算爆炸方向
        const angle = (Math.PI * 2 * i) / particleCount;
        const velocity = 100 + Math.random() * 100;
        const endX = x + Math.cos(angle) * velocity;
        const endY = y + Math.sin(angle) * velocity;
        
        // 动画
        const animation = particle.animate([
            {
                transform: 'translate(0, 0) scale(1)',
                opacity: 1
            },
            {
                transform: `translate(${endX - x}px, ${endY - y + 50}px) scale(0)`,
                opacity: 0
            }
        ], {
            duration: 1000 + Math.random() * 500,
            easing: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)'
        });
        
        animation.onfinish = () => {
            particle.remove();
        };
    }
    
    // 创建中心闪光
    createFireworkFlash(x, y);
}

function createFireworkFlash(x, y) {
    const flash = document.createElement('div');
    flash.style.cssText = `
        position: absolute;
        width: 20px;
        height: 20px;
        background: radial-gradient(circle, white, transparent);
        border-radius: 50%;
        left: ${x - 10}px;
        top: ${y - 10}px;
        pointer-events: none;
    `;
    
    document.getElementById('fireworks-container').appendChild(flash);
    
    flash.animate([
        {
            transform: 'scale(0)',
            opacity: 1
        },
        {
            transform: 'scale(5)',
            opacity: 0
        }
    ], {
        duration: 300,
        easing: 'ease-out'
    }).onfinish = () => {
        flash.remove();
    };
}

// 连续烟花效果
function createContinuousFireworks(duration = 5000) {
    const startTime = Date.now();
    
    function createRandomFirework() {
        if (Date.now() - startTime < duration) {
            createSingleFirework();
            setTimeout(createRandomFirework, 200 + Math.random() * 800);
        }
    }
    
    createRandomFirework();
}

// 心形烟花
function createHeartFirework() {
    const container = document.getElementById('fireworks-container');
    if (!container) return;
    
    const centerX = window.innerWidth / 2;
    const centerY = window.innerHeight / 2;
    
    // 心形路径点
    const heartPoints = [];
    for (let t = 0; t < Math.PI * 2; t += 0.1) {
        const x = 16 * Math.pow(Math.sin(t), 3);
        const y = -(13 * Math.cos(t) - 5 * Math.cos(2*t) - 2 * Math.cos(3*t) - Math.cos(4*t));
        heartPoints.push({
            x: centerX + x * 3,
            y: centerY + y * 3
        });
    }
    
    // 在心形路径上创建烟花
    heartPoints.forEach((point, index) => {
        setTimeout(() => {
            createFireworkExplosion(point.x, point.y);
        }, index * 50);
    });
}

// 文字烟花
function createTextFirework(text, x, y) {
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    canvas.width = 200;
    canvas.height = 100;
    
    ctx.font = '40px Arial';
    ctx.fillStyle = 'white';
    ctx.textAlign = 'center';
    ctx.fillText(text, 100, 60);
    
    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    const pixels = imageData.data;
    
    for (let i = 0; i < pixels.length; i += 4) {
        if (pixels[i + 3] > 128) { // alpha > 128
            const pixelX = (i / 4) % canvas.width;
            const pixelY = Math.floor((i / 4) / canvas.width);
            
            if (Math.random() < 0.3) { // 稀疏化
                setTimeout(() => {
                    createSmallFirework(x + pixelX - 100, y + pixelY - 50);
                }, Math.random() * 2000);
            }
        }
    }
}

function createSmallFirework(x, y) {
    const colors = ['#ff6b9d', '#4ecdc4', '#ffe66d'];
    const color = colors[Math.floor(Math.random() * colors.length)];
    
    const particle = document.createElement('div');
    particle.style.cssText = `
        position: absolute;
        width: 4px;
        height: 4px;
        background: ${color};
        border-radius: 50%;
        left: ${x}px;
        top: ${y}px;
        pointer-events: none;
        box-shadow: 0 0 8px ${color};
    `;
    
    document.getElementById('fireworks-container').appendChild(particle);
    
    particle.animate([
        {
            transform: 'scale(0)',
            opacity: 1
        },
        {
            transform: 'scale(2)',
            opacity: 0
        }
    ], {
        duration: 800,
        easing: 'ease-out'
    }).onfinish = () => {
        particle.remove();
    };
}

// 螺旋烟花
function createSpiralFirework() {
    const centerX = window.innerWidth / 2;
    const centerY = window.innerHeight / 2;
    
    for (let i = 0; i < 50; i++) {
        setTimeout(() => {
            const angle = (i * Math.PI) / 10;
            const radius = i * 5;
            const x = centerX + Math.cos(angle) * radius;
            const y = centerY + Math.sin(angle) * radius;
            
            createFireworkExplosion(x, y);
        }, i * 100);
    }
}

// 清理所有烟花
function clearFireworks() {
    const container = document.getElementById('fireworks-container');
    if (container) {
        container.innerHTML = '';
    }
}

